/**
 * 调度座位管理初始化
 */
var TrainSeatSchedule = {
    id: "TrainSeatScheduleTable",	//表格id
    seItem: null,		//选中的条目
    ids:new Array(),
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TrainSeatSchedule.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '调度车次Id', field: 'staId', visible: false, align: 'center', valign: 'middle'},
        {title: '车厢号', field: 'carriageNo', visible: true, align: 'center', valign: 'middle'},
        {title: '座位号', field: 'seatNo', visible: true, align: 'center', valign: 'middle'},
        {title: '座位类型', field: 'seatType', visible: false, align: 'center', valign: 'middle'},
        {title: '座位类型', field: 'seatTypeName', visible: true, align: 'center', valign: 'middle'},
        {title: '座位状态', field: 'seatStatus', visible: false, align: 'center', valign: 'middle'},
        {title: '座位状态', field: 'seatStatusName', visible: true, align: 'center', valign: 'middle'},
        {title: '标识', field: 'identification', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
TrainSeatSchedule.check = function () {
	debugger
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        TrainSeatSchedule.seItem = selected[0];
        for (var i=0; i<selected.length; i++){
        	TrainSeatSchedule.ids[i] = selected[i].id;
        }
        return true;
    }
};

/**
 * 检查是否选中（不包含座位状态为已锁定的记录）
 */
TrainSeatSchedule.lockedcheck = function () {
	debugger
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        TrainSeatSchedule.seItem = selected[0];
        for (var i=0; i<selected.length; i++){
        	if(selected[i].seatStatus != 0){
        		Feng.info("请勿选择已经锁定的座位！");
        		return false;
        		break;
        	}
        	TrainSeatSchedule.ids[i] = selected[i].id;
        }
        return true;
    }
};

/**
 * 检查是否选中（不包含座位状态为可选的，标识不是人工锁定的记录）
 */
TrainSeatSchedule.unlockedcheck = function () {
	debugger
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        TrainSeatSchedule.seItem = selected[0];
        for (var i=0; i<selected.length; i++){
        	if(selected[i].seatStatus == 0 || selected[i].identification !='LOCKED'){
        		Feng.info("请勿选择未锁定或者标识不是LOCKED的座位！");
        		return false;
        		break;
        	}
        	TrainSeatSchedule.ids[i] = selected[i].id;
        }
        return true;
    }
};

/**
 * 点击添加调度座位
 */
TrainSeatSchedule.openAddTrainSeatSchedule = function () {
    var index = layer.open({
        type: 2,
        title: '添加调度座位',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/trainSeatSchedule/trainSeatSchedule_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看调度座位详情
 */
TrainSeatSchedule.openTrainSeatScheduleDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '调度座位详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/trainSeatSchedule/trainSeatSchedule_update/' + TrainSeatSchedule.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 锁定调度座位
 */
TrainSeatSchedule.lock = function () {
    if (this.lockedcheck()) {
        var ajax = new $ax(Feng.ctxPath + "/ticketRemainder/lock", function (data) {
            Feng.success("锁定成功!");
            TrainSeatSchedule.table.refresh();
            TrainSeatSchedule.ids =new Array();
        }, function (data) {
            Feng.error("锁定失败!" + data.responseJSON.message + "!");
        });
        ajax.set("trainSeatScheduleIds",JSON.stringify(this.ids));
        ajax.set("ticketRemainderId",$("#ticketRemainderId").val());
        ajax.start();
    }
};

/**
 * 取消锁定调度座位
 */
TrainSeatSchedule.canclelock = function () {
    if (this.unlockedcheck()) {
        var ajax = new $ax(Feng.ctxPath + "/ticketRemainder/canclelock", function (data) {
            Feng.success("取消锁定成功!");
            TrainSeatSchedule.table.refresh();
            TrainSeatSchedule.ids =new Array();
        }, function (data) {
            Feng.error("取消锁定失败!" + data.responseJSON.message + "!");
        });
        ajax.set("trainSeatScheduleIds",JSON.stringify(this.ids));
        ajax.set("ticketRemainderId",$("#ticketRemainderId").val());
        ajax.start();
    }
};

/**
 * 查询调度座位列表
 */
TrainSeatSchedule.search = function () {
    var queryData = {};
    queryData['ticketRemainderId'] = $("#ticketRemainderId").val();
    queryData['carriageNo'] = $("#carriageNo").val();
    queryData['seatNo'] = $("#seatNo").val();
    queryData['seatType'] = $("#seatType").val();
    TrainSeatSchedule.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = TrainSeatSchedule.initColumn();
    var ticketRemainderId = $("#ticketRemainderId").val();
    var table = new BSTable(TrainSeatSchedule.id, "/ticketRemainder/seatDetail/list/"+ticketRemainderId, defaultColunms);
    table.setPaginationType("client");
    TrainSeatSchedule.table = table.init();
});
